package com.ruoyi.place.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.service.TcCommonService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.place.domain.PlaceEzdhxpqy;
import com.ruoyi.place.domain.bo.PlaceEzdhxpqyBo;
import com.ruoyi.place.domain.vo.PlaceEzdhxpqyVo;
import com.ruoyi.place.mapper.PlaceEzdhxpqyMapper;
import com.ruoyi.place.service.IPlaceEzdhxpqyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 易制毒化学品企业Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class PlaceEzdhxpqyServiceImpl implements IPlaceEzdhxpqyService {

    private final PlaceEzdhxpqyMapper baseMapper;
    private final TcCommonService tcCommonService;

    /**
     * 查询易制毒化学品企业
     */
    @Override
    public PlaceEzdhxpqyVo queryById(Long ezdhxpqyId) {
        PlaceEzdhxpqyVo result = baseMapper.selectVoById(ezdhxpqyId);
        tcCommonService.renderTcInfo(Lists.newArrayList(result));
        return result;
    }

    /**
     * 查询易制毒化学品企业列表
     */
    @Override
    public TableDataInfo<PlaceEzdhxpqyVo> queryPageList(PlaceEzdhxpqyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PlaceEzdhxpqy> lqw = buildQueryWrapper(bo);
        Page<PlaceEzdhxpqyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        tcCommonService.renderTcInfo(result.getRecords());
        return TableDataInfo.build(result);
    }

    /**
     * 查询易制毒化学品企业列表
     */
    @Override
    public List<PlaceEzdhxpqyVo> queryList(PlaceEzdhxpqyBo bo) {
        LambdaQueryWrapper<PlaceEzdhxpqy> lqw = buildQueryWrapper(bo);
        List<PlaceEzdhxpqyVo> result = baseMapper.selectVoList(lqw);
        tcCommonService.renderTcInfo(result);
        return result;
    }

    private LambdaQueryWrapper<PlaceEzdhxpqy> buildQueryWrapper(PlaceEzdhxpqyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PlaceEzdhxpqy> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), PlaceEzdhxpqy::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getZhzt()), PlaceEzdhxpqy::getZhzt, bo.getZhzt());
        lqw.eq(StringUtils.isNotBlank(bo.getDjjdshzt()), PlaceEzdhxpqy::getDjjdshzt, bo.getDjjdshzt());
        return lqw;
    }

    /**
     * 新增易制毒化学品企业
     */
    @Override
    public Boolean insertByBo(PlaceEzdhxpqyBo bo) {
        PlaceEzdhxpqy add = BeanUtil.toBean(bo, PlaceEzdhxpqy.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEzdhxpqyId(add.getEzdhxpqyId());
        }
        return flag;
    }

    /**
     * 修改易制毒化学品企业
     */
    @Override
    public Boolean updateByBo(PlaceEzdhxpqyBo bo) {
        PlaceEzdhxpqy update = BeanUtil.toBean(bo, PlaceEzdhxpqy.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PlaceEzdhxpqy entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除易制毒化学品企业
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
